package com.cjc.sport.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 文件工具类
 */
public final class FileUtil {

    /**
     * 读取输入流
     * @param in 输入流
     * @return 字符串
     * @throws Exception 异常
     */
    public static String readAll(InputStream in) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        StringBuffer sb = new StringBuffer();
        String ln = null;

        while (null != (ln = reader.readLine())) {
            sb.append(ln);
        }

        return sb.toString();
    }

    /**
     * 读取输入流
     * @param in 输入流
     * @param charsetName 字符名字
     * @return 字符串
     * @throws Exception 异常
     */
    public static String readAll(InputStream in, String charsetName) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, charsetName));
        StringBuffer sb = new StringBuffer();
        String ln = null;

        while (null != (ln = reader.readLine())) {
            sb.append(ln);
        }


        return sb.toString();
    }

    /**
     * 读取输入流
     * @param reader 字节流
     * @return 字符串
     */
    public static String readAll(BufferedReader reader) {
        try {
            StringBuffer sb = new StringBuffer();
            String ln = null;

            while (null != (ln = reader.readLine())) {
                sb.append(ln);
            }

            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


}
